package com.lfs.admin.service.impl;

import cn.hutool.json.JSONObject;
import com.github.pagehelper.PageHelper;
import com.google.common.collect.Lists;
import com.lfs.admin.dao.DataReleaseTaskDao;
import com.lfs.admin.dao.DataRepositoryDao;
import com.lfs.admin.dao.DataUploadDao;
import com.lfs.admin.model.entity.dataPackage.DataReleaseDetailEntity;
import com.lfs.admin.model.entity.dataPackage.DataReleaseTaskEntity;
import com.lfs.admin.model.entity.dataPackage.DataRepositoryEntity;
import com.lfs.admin.model.vo.DataReleaseDetailVo;
import com.lfs.admin.model.vo.DataReleaseVo;
import com.lfs.admin.model.vo.DataRepositoryVo;
import com.lfs.admin.model.vo.dataPackage.DataUploadVo;
import com.lfs.admin.service.DataReleaseService;
import com.lfs.admin.service.DataRepositoryService;
import com.lfs.admin.service.system.TokenService;
import com.lfs.base.util.StringUtils;
import com.lfs.common.core.domain.entity.SysUser;
import com.lfs.common.core.domain.model.LoginUser;
import com.lfs.common.utils.ServletUtils;
import com.lfs.dao.entity.PageBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DataReleaseServiceImpl implements DataReleaseService {

    private final Logger logger = LoggerFactory.getLogger(DataReleaseServiceImpl.class);

    @Autowired
    private DataReleaseTaskDao dataReleaseTaskDao;

    @Autowired
    private TokenService tokenService;

    @Override
    public PageBean<DataReleaseTaskEntity> queryDataReleaseList(DataReleaseVo dataReleaseVo) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        SysUser user = loginUser.getUser();
        String agtNo = StringUtils.isBlank(user.getUserName()) ? "error" : user.getUserName();
        dataReleaseVo.setAgtNo(agtNo);
        List<DataReleaseTaskEntity> dataReleaseTaskEntities = Lists.newArrayList();
        try {
            PageHelper.startPage(dataReleaseVo.getCurrentPage(), dataReleaseVo.getPageSize());
            dataReleaseTaskEntities = dataReleaseTaskDao.queryDataReleaseList(dataReleaseVo);

        } catch (Exception e) {
            logger.error("查询数据仓库列表异常={}",e.getMessage(),e);
        }
        return new PageBean<DataReleaseTaskEntity>(dataReleaseTaskEntities);
    }

    @Override
    public PageBean<DataReleaseDetailEntity> queryDataReleaseDetailList(DataReleaseDetailVo dataReleaseDetailVo) {
        List<DataReleaseDetailEntity> dataReleaseDetailEntities = Lists.newArrayList();
        try {
            PageHelper.startPage(dataReleaseDetailVo.getCurrentPage(), dataReleaseDetailVo.getPageSize());
            dataReleaseDetailEntities = dataReleaseTaskDao.queryDataReleaseDetailList(dataReleaseDetailVo);

        } catch (Exception e) {
            logger.error("查询数据仓库列表异常={}",e.getMessage(),e);
        }
        return new PageBean<DataReleaseDetailEntity>(dataReleaseDetailEntities);
    }

    @Override
    public int deleteDataRelease(Integer[] ids) {
        return dataReleaseTaskDao.deleteDataReleaseByIds(ids);
    }

}
